import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import config.GenerateParam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * @Author: fxy
 * @Date: 2021/12/27/17:17
 * @Description:
 */
public class Generate {
    public static void main(String[] args) throws IOException {

        String path = "D:\\project\\myproject\\test\\test03-generate\\src\\main\\resources\\generate.properties";
        File file = new File(path);
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String username = properties.getProperty("username");
        String filePath = file.getParentFile().getParent()+"\\" + "java";
        String packagePath = properties.getProperty("packagePath");
        String xmlPath = file.getParentFile().getPath()+"\\"+properties.getProperty("xmlPath");
        System.out.println("filePath" + xmlPath);
        GenerateParam generateParam = new GenerateParam(url,username,password,filePath,packagePath,xmlPath);
        generate(generateParam);

    }
    public static void generate(GenerateParam generateParam){
            List<String> tables = new ArrayList<>();
            tables.add("user");

            FastAutoGenerator.create(generateParam.getUrl(), generateParam.getUsername(), generateParam.getPassword())
                    .globalConfig(builder -> {
                        builder.author("yun")               //作者
                                .outputDir(generateParam.getFilePath())    //输出路径(写到java目录)
                                .enableSwagger()           //开启swagger
                                .commentDate("yyyy-MM-dd")
                                .fileOverride();            //开启覆盖之前生成的文件

                    })
                    .packageConfig(builder -> {
                        builder.parent(generateParam.getPackagePath())
                                .moduleName("practice")
                                .entity("entity")
                                .service("service")
                                .controller("controller")
                                .mapper("mapper")
                                .xml("mapper")
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"));
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude(tables)
                                .addTablePrefix("p_")
                                .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .entityBuilder()
                                .enableLombok()
                                .logicDeleteColumnName("deleted")
                                .enableTableFieldAnnotation()
                                .controllerBuilder()
                                .formatFileName("%sController")
                                .enableRestStyle()
                                .mapperBuilder()
                                .superClass(BaseMapper.class)
                                .formatMapperFileName("%sMapper")
                                .enableMapperAnnotation()
                                .formatXmlFileName("%sMapper");
                    }).templateConfig(builder -> {
                        builder.mapperXml("templates/mapper.xml.vm")
                        .entity("");
            })
                    .execute();

        }

}